package com.maas.util.mybatis.jpa.littlenb.support.template.select;

import com.littlenb.mybatisjpa.support.template.SqlTemplate;
import com.littlenb.mybatisjpa.util.ColumnMetaResolver;
import com.littlenb.mybatisjpa.util.PersistentUtil;

import java.lang.reflect.Field;

/**
 *  忽略空值
 * 动态生成<where>，还没实现
 * SelectIgnoreNullSqlTemplate
 * @author sway.li
 **/
public class SelectIgnoreNullSqlTemplate implements SqlTemplate {

  public static final SqlTemplate INSTANCE = new SelectIgnoreNullSqlTemplate();

  public String[] neFeildArr =null;

  @Override
  public String parseSQL(Class<?> type) {
    // columns
    StringBuilder sets = new StringBuilder();
    sets.append("<trim prefix=\"WHERE\" prefixOverrides=\"AND\">");
    for (Field field : PersistentUtil.getPersistentFields(type)) {

      sets.append("<if test='" + field.getName() + "!= null'> ");
      // columnName = #{ }
      sets.append("and ");
      sets.append(PersistentUtil.getColumnName(field)).append(" = ")
              .append(ColumnMetaResolver.resolveSqlPlaceholder(field));

      sets.append("</if> ");
    }

    sets.append("</trim> ");

    return   sets.toString();
  }

}
